package com.hassean.we.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hassean.we.admin.common.constant.CommonConstant;
import com.hassean.we.admin.common.util.Query;
import com.hassean.we.admin.common.util.R;
import com.hassean.we.admin.common.web.BaseController;
import com.hassean.we.admin.model.entity.TbTerminal;
import com.hassean.we.admin.model.entity.TbTerminalLocation;
import com.hassean.we.admin.service.TbSimInfoService;
import com.hassean.we.admin.service.TbTerminalLocationService;
import com.hassean.we.admin.service.TbTerminalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * 备注：最新的轨迹
 * </p>
 *
 * @author yajun
 * @since 2019-03-29
 */
@Api(description = "终端位置接口")
@RestController
@RequestMapping("/tbTerminalLocation")
public class TbTerminalLocationController extends BaseController {
    @Autowired
    private TbTerminalLocationService tbTerminalLocationService;
    @Autowired
    private TbTerminalService tbTerminalService;
    @Autowired
    private TbSimInfoService tbSimInfoService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return TbTerminalLocation
     */
    @ApiOperation(value = "通过ID查询终端位置" ,  notes="通过ID查询终端位置")
    @GetMapping("/{id}")
    public R<TbTerminalLocation> get(@PathVariable Long id) {
        return new R<>(tbTerminalLocationService.selectById(id));
    }


    /**
     * 分页查询信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @ApiOperation(value = "分页查询终端位置" ,  notes="分页查询终端位置")
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return tbTerminalLocationService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 添加
     *
     * @param tbTerminalLocation 实体
     * @return success/false
     */
    @ApiOperation(value = "添加终端位置" ,  notes="添加终端位置")
    @PostMapping
    public R<Boolean> add(@RequestBody TbTerminalLocation tbTerminalLocation) {
        return new R<>(tbTerminalLocationService.insert(tbTerminalLocation));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @ApiOperation(value = "删除终端位置" ,  notes="删除终端位置")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        TbTerminalLocation tbTerminalLocation = new TbTerminalLocation();
        tbTerminalLocation.setTerminalLocationId(id);
        tbTerminalLocation.setUpdateTime(new Date());
        tbTerminalLocation.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(tbTerminalLocationService.updateById(tbTerminalLocation));
    }

    /**
     * 编辑
     *
     * @param tbTerminalLocation 实体
     * @return success/false
     */
    @ApiOperation(value = "编辑终端位置" ,  notes="编辑终端位置")
    @PutMapping
    public R<Boolean> edit(@RequestBody TbTerminalLocation tbTerminalLocation) {
        tbTerminalLocation.setUpdateTime(new Date());
        return new R<>(tbTerminalLocationService.updateById(tbTerminalLocation));
    }

    /**
     * 获取终端位置信息列表
     *
     * @return success/false
     */
    @ApiOperation(value = "获取终端位置信息列表" ,  notes="获取终端位置信息列表")
    @RequestMapping("/list")
    public R<Map<String, Object>> showtbTerminalLocationList(@RequestParam Map<String, Object> params) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> resultMap = new HashMap<>();
        EntityWrapper entity = new EntityWrapper<>();
        //手机号码查询终端位置
        if (params.get("phone") != null && !(params.get("phone")).equals("")) {
            String passivityPhone = params.get("phone").toString();//电话号码
            params.remove("phone");
            //通过电话号码获取终端
            List<Map<String, Object>> list = tbTerminalService.selectTerminalIdByPhone(passivityPhone);
            //获取被叫终端id
            if (!list.isEmpty()) {
                entity.eq("terminal_id", list.get(0).get("terminal_id"));

            }

        }
        if (params.get("terminalNumber") != null && !(params.get("terminalNumber")).equals("")) {
            String terminalNumber = params.get("terminalNumber").toString();//终端号码
            //通过终端号码查询终端位置
            EntityWrapper en = new EntityWrapper<>();
            en.eq("terminal_number", terminalNumber);
            TbTerminal tbTerminal = tbTerminalService.selectOne(en);
            if (tbTerminal != null) {
                entity.eq("terminal_id", tbTerminal.getTerminalId());

            }


        }
        List<TbTerminalLocation> selectList = tbTerminalLocationService.selectList(entity);
        if (!selectList.isEmpty()) {
            for (int i = 0; i < selectList.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                //map.put("tbTerminalLocation", selectList.get(i));
                map.put("terminalLocationId", selectList.get(i).getTerminalLocationId());
                map.put("longitude", selectList.get(i).getLongitude());
                map.put("latitude", selectList.get(i).getLatitude());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String addTime = sdf.format(selectList.get(i).getAddTime());
                map.put("addTime", addTime);

                map.put("temp", selectList.get(i).getTemp());
                map.put("status", selectList.get(i).getStatus());
                map.put("remark", selectList.get(i).getRemark());
                map.put("advanceText", selectList.get(i).getAdvanceText());
                map.put("advanceDate", selectList.get(i).getAdvanceDate());
                map.put("delFlag", selectList.get(i).getDelFlag());

                Long terminalId = selectList.get(i).getTerminalId();//终端id
                if (terminalId != null) {
                    TbTerminal tbTerminal = tbTerminalService.selectById(terminalId);
                    if (tbTerminal != null) {
                        map.put("tbTerminal", tbTerminal);
                    }
                }
                resultList.add(map);
            }
        }
        resultMap.put("total", selectList.size());//总条数
        resultMap.put("resultList", resultList);


        return new R<>(resultMap);
    }

}